/**
 * Copyright &copy; 2012-2017 <a href="https://vivo.com">vivo</a> All rights reserved.
 */
package com.vivo.system.service;

import java.util.List;

import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.service.OfficeService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.vivo.employee.entity.ViEmployee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.vivo.system.entity.VivoPartTimeJob;
import com.vivo.system.dao.VivoPartTimeJobDao;

/**
 * 员工兼职Service
 * @author jyangye
 * @version 2018-03-22
 */
@Service
@Transactional(readOnly = true)
public class VivoPartTimeJobService extends CrudService<VivoPartTimeJobDao, VivoPartTimeJob> {

    @Autowired
    private VivoPartTimeJobDao vivoPartTimeJobDao;

    @Autowired
    private OfficeService officeService;

	public VivoPartTimeJob get(String id) {
		return super.get(id);
	}
	
	public List<VivoPartTimeJob> findList(VivoPartTimeJob vivoPartTimeJob) {
		return super.findList(vivoPartTimeJob);
	}

	public List<VivoPartTimeJob> findListByEmpId(String eId) {
	    return vivoPartTimeJobDao.findListByEmpId(eId);
    }
	
	public Page<VivoPartTimeJob> findPage(Page<VivoPartTimeJob> page, VivoPartTimeJob vivoPartTimeJob) {
		return super.findPage(page, vivoPartTimeJob);
	}

    /**
     * 设置用户兼职信息
     * @param ve
     * @param post
     */
    @Transactional(readOnly = false)
	public Office assignPostToEmp(ViEmployee ve, Office post) {
        if (ve == null) {
            System.out.println("emp is null");
            return null;
        }

        if (post == null) {
            System.out.println("post is null");
            return null;
        }

        Office dept = officeService.getSuperDept(post);
        if (dept == null) {
            System.out.println("dept is null");
            return null;
        }

        Office comp = officeService.getSuperComp(dept);
        if (comp == null) {
            System.out.println("comp is null");
            return null;
        }

        VivoPartTimeJob vivoPartTimeJob = new VivoPartTimeJob();
        vivoPartTimeJob.setComp(comp);
        vivoPartTimeJob.setDept(dept);
        vivoPartTimeJob.setPost(post);
        vivoPartTimeJob.setEmployeeId(ve.getId());
        vivoPartTimeJob.setOperator(UserUtils.getUser().getName());
        vivoPartTimeJob.setIsExp("0");

        save(vivoPartTimeJob);

	    return post;
    }


    /**
     * 删除用户兼职信息
     * @param eId
     * @param postId
     * @return
     */
    @Transactional(readOnly = false)
    public Boolean outPostInEmp(String eId, String postId) {
        List<VivoPartTimeJob> jobList = findListByEmpId(eId);
        Office comp = UserUtils.getUser().getCompany();

        for (VivoPartTimeJob job : jobList) {
            if (job.getPost().getId().equals(postId)) {
                // 不允许二级删除外派用户信息
                if (job.getIsExp().equals("1")) {
                    if (!comp.getName().equals("百胜通讯")) {
                        return false;
                    }
                }

                delete(job);
                return true;
            }
        }

        return false;
    }

	
	@Transactional(readOnly = false)
	public void save(VivoPartTimeJob vivoPartTimeJob) {
		super.save(vivoPartTimeJob);
	}
	
	@Transactional(readOnly = false)
	public void delete(VivoPartTimeJob vivoPartTimeJob) {
		super.delete(vivoPartTimeJob);
	}
	
}